/*     */ package blockbuster;
/*     */ import blockbuster.components.BedrockComponentBase;
/*     */ import blockbuster.components.appearance.BedrockComponentAppearanceLighting;
/*     */ import blockbuster.components.expiration.BedrockComponentExpireInBlocks;
/*     */ import blockbuster.components.expiration.BedrockComponentExpireNotInBlocks;
/*     */ import blockbuster.components.expiration.BedrockComponentParticleLifetime;
/*     */ import blockbuster.components.meta.BedrockComponentLocalSpace;
/*     */ import blockbuster.components.motion.BedrockComponentInitialSpin;
/*     */ import blockbuster.components.motion.BedrockComponentMotionDynamic;
/*     */ import blockbuster.components.motion.BedrockComponentMotionParametric;
/*     */ import blockbuster.components.rate.BedrockComponentRateSteady;
/*     */ import blockbuster.components.shape.BedrockComponentShapeDisc;
/*     */ import blockbuster.math.Operation;
/*     */ import blockbuster.math.molang.MolangException;
/*     */ import com.google.common.collect.BiMap;
/*     */ import com.google.common.collect.HashBiMap;
/*     */ import com.google.gson.JsonDeserializationContext;
/*     */ import com.google.gson.JsonDeserializer;
/*     */ import com.google.gson.JsonElement;
/*     */ import com.google.gson.JsonObject;
/*     */ import com.google.gson.JsonParseException;
/*     */ import com.google.gson.JsonPrimitive;
/*     */ import com.google.gson.JsonSerializationContext;
/*     */ import com.google.gson.JsonSerializer;
/*     */ import java.lang.reflect.Type;
/*     */ import java.util.Map;
/*     */ 
/*     */ public class BedrockSchemeJsonAdapter implements JsonDeserializer<BedrockScheme>, JsonSerializer<BedrockScheme> {
/*  29 */   public BiMap<String, Class<? extends BedrockComponentBase>> components = (BiMap<String, Class<? extends BedrockComponentBase>>)HashBiMap.create();
/*     */   
/*     */   public static boolean isEmpty(JsonElement element) {
/*  32 */     if (element.isJsonArray())
/*  33 */       return (element.getAsJsonArray().size() == 0); 
/*  34 */     if (element.isJsonObject())
/*  35 */       return (element.getAsJsonObject().size() == 0); 
/*  36 */     if (element.isJsonPrimitive()) {
/*  37 */       JsonPrimitive primitive = element.getAsJsonPrimitive();
/*     */       
/*  39 */       if (primitive.isString())
/*  40 */         return primitive.getAsString().isEmpty(); 
/*  41 */       if (primitive.isNumber()) {
/*  42 */         return Operation.equals(primitive.getAsDouble(), 0.0D);
/*     */       }
/*     */     } 
/*     */     
/*  46 */     return element.isJsonNull();
/*     */   }
/*     */ 
/*     */   
/*     */   public BedrockSchemeJsonAdapter() {
/*  51 */     this.components.put("minecraft:emitter_local_space", BedrockComponentLocalSpace.class);
/*  52 */     this.components.put("minecraft:emitter_initialization", BedrockComponentInitialization.class);
/*     */ 
/*     */     
/*  55 */     this.components.put("minecraft:emitter_rate_instant", BedrockComponentRateInstant.class);
/*  56 */     this.components.put("minecraft:emitter_rate_steady", BedrockComponentRateSteady.class);
/*     */ 
/*     */     
/*  59 */     this.components.put("minecraft:emitter_lifetime_looping", BedrockComponentLifetimeLooping.class);
/*  60 */     this.components.put("minecraft:emitter_lifetime_once", BedrockComponentLifetimeOnce.class);
/*  61 */     this.components.put("minecraft:emitter_lifetime_expression", BedrockComponentLifetimeExpression.class);
/*     */ 
/*     */     
/*  64 */     this.components.put("minecraft:emitter_shape_disc", BedrockComponentShapeDisc.class);
/*  65 */     this.components.put("minecraft:emitter_shape_box", BedrockComponentShapeBox.class);
/*  66 */     this.components.put("minecraft:emitter_shape_entity_aabb", BedrockComponentShapeEntityAABB.class);
/*  67 */     this.components.put("minecraft:emitter_shape_point", BedrockComponentShapePoint.class);
/*  68 */     this.components.put("minecraft:emitter_shape_sphere", BedrockComponentShapeSphere.class);
/*     */ 
/*     */     
/*  71 */     this.components.put("minecraft:particle_lifetime_expression", BedrockComponentParticleLifetime.class);
/*  72 */     this.components.put("minecraft:particle_expire_if_in_blocks", BedrockComponentExpireInBlocks.class);
/*  73 */     this.components.put("minecraft:particle_expire_if_not_in_blocks", BedrockComponentExpireNotInBlocks.class);
/*  74 */     this.components.put("minecraft:particle_kill_plane", BedrockComponentKillPlane.class);
/*     */ 
/*     */     
/*  77 */     this.components.put("minecraft:particle_appearance_billboard", BedrockComponentAppearanceBillboard.class);
/*  78 */     this.components.put("minecraft:particle_appearance_lighting", BedrockComponentAppearanceLighting.class);
/*  79 */     this.components.put("minecraft:particle_appearance_tinting", BedrockComponentAppearanceTinting.class);
/*  80 */     this.components.put("blockbuster:particle_collision_appearance", BedrockComponentCollisionAppearance.class);
/*  81 */     this.components.put("blockbuster:particle_collision_tinting", BedrockComponentCollisionTinting.class);
/*     */ 
/*     */ 
/*     */ 
/*     */     
/*  86 */     this.components.put("minecraft:particle_initial_speed", BedrockComponentInitialSpeed.class);
/*  87 */     this.components.put("minecraft:particle_initial_spin", BedrockComponentInitialSpin.class);
/*  88 */     this.components.put("minecraft:particle_motion_collision", BedrockComponentMotionCollision.class);
/*  89 */     this.components.put("minecraft:particle_motion_dynamic", BedrockComponentMotionDynamic.class);
/*  90 */     this.components.put("minecraft:particle_motion_parametric", BedrockComponentMotionParametric.class);
/*     */   }
/*     */ 
/*     */   
/*     */   public BedrockScheme deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
/*  95 */     BedrockScheme scheme = new BedrockScheme();
/*     */     
/*  97 */     if (!json.isJsonObject()) {
/*  98 */       throw new JsonParseException("The root element of Bedrock particle should be an object!");
/*     */     }
/*     */ 
/*     */     
/* 102 */     JsonObject root = json.getAsJsonObject();
/*     */     
/*     */     try {
/* 105 */       parseEffect(scheme, getObject(root, "particle_effect", "No particle_effect was found..."));
/* 106 */     } catch (MolangException e) {
/* 107 */       throw new JsonParseException("Couldn't parse some MoLang expression!", e);
/*     */     } 
/*     */     
/* 110 */     scheme.setup();
/*     */     
/* 112 */     return scheme;
/*     */   }
/*     */   
/*     */   private void parseEffect(BedrockScheme scheme, JsonObject effect) throws JsonParseException, MolangException {
/* 116 */     parseDescription(scheme, getObject(effect, "description", "No particle_effect.description was found..."));
/*     */     
/* 118 */     if (effect.has("curves")) {
/* 119 */       JsonElement curves = effect.get("curves");
/*     */       
/* 121 */       if (curves.isJsonObject()) {
/* 122 */         parseCurves(scheme, curves.getAsJsonObject());
/*     */       }
/*     */     } 
/*     */     
/* 126 */     parseComponents(scheme, getObject(effect, "components", "No particle_effect.components was found..."));
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void parseDescription(BedrockScheme scheme, JsonObject description) throws JsonParseException {
/* 133 */     if (description.has("identifier")) {
/* 134 */       scheme.identifier = description.get("identifier").getAsString();
/*     */     }
/*     */     
/* 137 */     JsonObject parameters = getObject(description, "basic_render_parameters", "No particle_effect.basic_render_parameters was found...");
/*     */     
/* 139 */     if (parameters.has("material")) {
/* 140 */       scheme.material = BedrockMaterial.fromString(parameters.get("material").getAsString());
/*     */     }
/*     */     
/* 143 */     if (parameters.has("texture")) {
/* 144 */       String texture = parameters.get("texture").getAsString();
/*     */       
/* 146 */       if (!texture.equals("textures/particle/particles")) {
/* 147 */         scheme.texture = RLUtils.create(texture);
/*     */       }
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void parseCurves(BedrockScheme scheme, JsonObject curves) throws MolangException {
/* 156 */     for (Map.Entry<String, JsonElement> entry : (Iterable<Map.Entry<String, JsonElement>>)curves.entrySet()) {
/* 157 */       JsonElement element = entry.getValue();
/*     */       
/* 159 */       if (element.isJsonObject()) {
/* 160 */         BedrockCurve curve = new BedrockCurve();
/*     */         
/* 162 */         curve.fromJson(element.getAsJsonObject(), scheme.parser);
/* 163 */         scheme.curves.put(entry.getKey(), curve);
/*     */       } 
/*     */     } 
/*     */   }
/*     */   
/*     */   private void parseComponents(BedrockScheme scheme, JsonObject components) throws MolangException {
/* 169 */     for (Map.Entry<String, JsonElement> entry : (Iterable<Map.Entry<String, JsonElement>>)components.entrySet()) {
/* 170 */       String key = entry.getKey();
/*     */       
/* 172 */       if (this.components.containsKey(key)) {
/* 173 */         BedrockComponentBase component = null;
/*     */         
/*     */         try {
/* 176 */           component = ((Class<BedrockComponentBase>)this.components.get(key)).getConstructor(new Class[0]).newInstance(new Object[0]);
/* 177 */         } catch (Exception exception) {}
/*     */ 
/*     */         
/* 180 */         if (component != null) {
/* 181 */           component.fromJson(entry.getValue(), scheme.parser);
/* 182 */           scheme.components.add(component); continue;
/*     */         } 
/* 184 */         System.out.println("Failed to parse given component " + key + " in " + scheme.identifier + "!");
/*     */       } 
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   private JsonObject getObject(JsonObject object, String key, String message) throws JsonParseException {
/* 192 */     if (!object.has(key) && !object.get(key).isJsonObject()) {
/* 193 */       throw new JsonParseException(message);
/*     */     }
/*     */     
/* 196 */     return object.get(key).getAsJsonObject();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public JsonElement serialize(BedrockScheme src, Type typeOfSrc, JsonSerializationContext context) {
/* 204 */     JsonObject object = new JsonObject();
/* 205 */     JsonObject effect = new JsonObject();
/*     */     
/* 207 */     object.addProperty("format_version", "1.10.0");
/* 208 */     object.add("particle_effect", (JsonElement)effect);
/*     */     
/* 210 */     addDescription(effect, src);
/* 211 */     addCurves(effect, src);
/* 212 */     addComponents(effect, src);
/*     */     
/* 214 */     return (JsonElement)object;
/*     */   }
/*     */   
/*     */   private void addDescription(JsonObject effect, BedrockScheme scheme) {
/* 218 */     JsonObject desc = new JsonObject();
/* 219 */     JsonObject render = new JsonObject();
/*     */     
/* 221 */     effect.add("description", (JsonElement)desc);
/*     */     
/* 223 */     desc.addProperty("identifier", scheme.identifier);
/* 224 */     desc.add("basic_render_parameters", (JsonElement)render);
/*     */     
/* 226 */     render.addProperty("material", scheme.material.id);
/* 227 */     render.addProperty("texture", "textures/particle/particles");
/*     */     
/* 229 */     if (scheme.texture != null && !scheme.texture.equals(BedrockScheme.DEFAULT_TEXTURE)) {
/* 230 */       render.addProperty("texture", "models/particle/" + scheme.texture);
/*     */     }
/*     */   }
/*     */   
/*     */   private void addCurves(JsonObject effect, BedrockScheme scheme) {
/* 235 */     JsonObject curves = new JsonObject();
/*     */     
/* 237 */     effect.add("curves", (JsonElement)curves);
/*     */     
/* 239 */     for (Map.Entry<String, BedrockCurve> entry : scheme.curves.entrySet()) {
/* 240 */       curves.add(entry.getKey(), ((BedrockCurve)entry.getValue()).toJson());
/*     */     }
/*     */   }
/*     */   
/*     */   private void addComponents(JsonObject effect, BedrockScheme scheme) {
/* 245 */     JsonObject components = new JsonObject();
/*     */     
/* 247 */     effect.add("components", (JsonElement)components);
/*     */     
/* 249 */     for (BedrockComponentBase component : scheme.components) {
/* 250 */       JsonElement element = component.toJson();
/*     */       
/* 252 */       this; if (isEmpty(element) && !component.canBeEmpty()) {
/*     */         continue;
/*     */       }
/*     */       
/* 256 */       components.add((String)this.components.inverse().get(component.getClass()), element);
/*     */     } 
/*     */   }
/*     */ }


/* Location:              D:\fanbian\[D][龙之核心]DragonCore-2.6.0.3.jar!\blockbuster\BedrockSchemeJsonAdapter.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */